var count = 0;

function handleInsert() {
	count++;
	var d = document.createElement("div");
	d.setAttribute("item", "group");
	d.setAttribute("count", count);
	d.id = "group_" + count;
	d.innerHTML = "<div class=\"divn\"><a style='cursor:hand;' href='javascript:void(0);' title=\"删 除\" onclick=\"javascript:handleM('group_"
			+ count + "')\">删除</a></div>" + divtxt(count);
	document.all.form1.appendChild(d);

	D("disporder_" + count).value = count;
}

function divtxt(c) {
	var txt = document.getElementById("group_0").innerHTML;
	txt = txt.replace("pid_0", "pid_" + c);
	txt = txt.replace("glevel_0", "glevel_" + c);
	txt = txt.replace("gname_0", "gname_" + c);
	txt = txt.replace("gintro_0", "gintro_" + c);
	txt = txt.replace("garea_0", "garea_" + c);
	txt = txt.replace("disporder_0", "disporder_" + c);
	txt = txt.replace("gnumber_0", "gnumber_" + c);
	txt = txt.replace("grating_0", "grating_" + c);
	txt = txt.replace("gbranch_0", "gbranch_" + c);
	txt = txt.replace("glinshi_0", "glinshi_" + c);
	txt = txt.replace("glinshi_0", "glinshi_" + c);

	return txt;
}

function handleM(id) {
	document.getElementById(id).removeNode(true);
}

function handleSubmit() {
	var rnt = true;

	var d = A("item", "group");

	for (var i = 0; i < d.length; i++) {
		var c = d[i].getAttribute("count");
		if (isblank(D("gname_" + c))) {
			alert("请填写部门/组名称");
			rnt = false;
			break;
		}
	}

	if (rnt && confirm("确定保存")) {
		var countG = "";

		for (var i = 0; i < d.length; i++) {

			var id = d[i].id;
			var c = id.substring(id.indexOf("_") + 1);
			countG += c + ",";
		}

		createHidden("countG", countG.substring(0, countG.length - 1));

		document.form1.action = context + "/system/group.do?method=saveX";
		document.form1.submit();
	}
}

function init(gname) {
	var tabbar = new dhtmlXTabBar("a_tabbar", "top");
	tabbar.setImagePath(context + "/resources/dhtmlx/codebase/imgs/");
	tabbar.enableAutoReSize(true);
	tabbar.addTab("a1", "子组/部门定义", "100px");
	tabbar.setTabActive("a1");

	// var toolbar = new dhtmlXToolbarObject("toolbarObj");
	var toolbar = tabbar.cells("a1").attachToolbar();
	toolbar.setIconsPath(context
			+ "/resources/dhtmlx/codebase/imgs/dhxtoolbar_dhx_imgs/");
	toolbar.addButton("save", 1, "提交保存", "save.gif", "save_dis.gif");
	toolbar.addButton("add", 2, "增加", "new.gif", "new_dis.gif");

	toolbar.attachEvent("onClick", function(id) {
				if (id == "save") {
                    handleSubmit();
				}else if(id == "add"){
					handleInsert();
				}
			});

	tabbar.setContent("a1", "content");
	var myForm = new dhtmlXForm("form1");
}